/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import vinciappli.Templates;
import javafx.geometry.HPos;
import javafx.scene.layout.LayoutInfo;
import javafx.scene.text.Font;
import javafx.scene.control.Label;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.input.MouseEvent;
import control.ControllerPrincipal;
import javafx.scene.control.Tooltip;
import javafx.scene.control.Button;
import javafx.animation.transition.TranslateTransition;
import javafx.animation.transition.ScaleTransition;
import view.ViewPrincipal;
import javafx.scene.Cursor;

/**
 * @author Sik
 */
public class CivilPlace extends Group
{
    public-read var x : Integer = 240;
    public-read var y : Integer = 290;
    public var civilPlaces : CardPlace[];
    public var bonusPlaces : Integer[] = [0,0,0,0,0,0];
    public var civilSlots : Group[];
    public var cards : String[];
    public-read var isReduced : Boolean = false;
    public-read var isActive : Boolean = true;
    var height = 320;
    var width = 660;

    public function resetCivilization()
    {
        var i : Integer = 0;
        delete cards;
        for(civ in ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getCivilizations())
        {
            cards[i*2] = "file:///{{Templates.CARD_PATH}}{{civ.getCivilization().getCaracteristic1().getType().getType()}}{{Templates.IMG_EXTENSION}}";
            cards[i*2+1] = "file:///{{Templates.CARD_PATH}}{{civ.getCivilization().getCaracteristic2().getType().getType()}}{{Templates.IMG_EXTENSION}}";
            i++;
        }
    }

    public function updateCivilization()
    {
        var i : Integer = 0;
        for(card in cards)
        {
            civilPlaces[i].url = card;
            i++;
        }
    }

    public function updateBonusPts()
    {
        var i : Integer = 0;
        for(civ in ControllerPrincipal.getINSTANCE().getControllerGameBoard().getGameBoard().getCivilizations())
        {
            bonusPlaces[i] = civ.getBonusPts();
            i++;
        }
    }


    public function setActive(activated : Boolean) : Void
    {
        if(activated)
        {
            isActive = true;
        }
        else
        {
            isActive = false;
        }
    }


    var window = Rectangle
            {

                height: height
                width: width
                fill: Color.WHITE
                arcHeight: 20
                arcWidth: 20
                opacity: 0.8
            }

    var title : Label =
    Label
    {
        layoutInfo: LayoutInfo
        {
            width: width
        }

        text: "CIVILISATIONS"
        textFill: Color.BLACK
        font : Font
        {
            name: "Impact"
            size: 30
        }
        hpos: HPos.CENTER
    }

    var reduceButton = Button {
        translateX: width - 40
        translateY: 5
        width: 13
        height: 13
	text: "_"
        cursor: Cursor.HAND
        font : Font
            {
                name: "Impact"
                size: 14
            }
            tooltip: Tooltip
            {
                text: "Réduire la fenêtre"
            }
	action: function()
        {
            miniaturized();
	}
    }

    def translateDown = TranslateTransition {
	duration: 1s
	node : this
	toX : 110 toY : -100
	repeatCount:1
    }

    def scaleDown = ScaleTransition {
	duration: 1s
	node : this
	toX: 0.17 toY: 0.17
	repeatCount:1
    }

    def translateUp = TranslateTransition {
	duration: 1s
	node : this
	toX : x toY : y
	repeatCount:1
    }

    def scaleUp = ScaleTransition {
	duration: 1s
	node : this
	toX: 1 toY: 1
	repeatCount:1
    }


    public function buildPlaces(): Void
    {
        var x : Integer = 10;
        var y : Integer = 40;
        var numCivil : String[] = ["I", "II", "III", "IV", "V", "VI"];
        var width : Integer = 200;
        var height : Integer = 120;

        resetCivilization();
        updateCivilization();

        for(i in [0..(numCivil.size()-1)])
        {
            var rect : Rectangle =
            Rectangle
            {
                x: x
                y: y
                height: height
                width: width
                arcWidth: 20 arcHeight: 20
                fill: null
                stroke: Color.BLACK
                strokeWidth: 4
                strokeLineCap: StrokeLineCap.ROUND
                pickOnBounds: true
                cursor: Cursor.HAND
            }

            x += 10;
            
            var card1 : CardPlace =
            bind CardPlace
                {
                    height: 85
                    width: 85
                    url: cards[i*2]
                    translateX: x
                    translateY: y
                }
                
            var card2 : CardPlace =
            bind CardPlace
                {
                    height: 85
                    width: 85
                    url: cards[i*2+1]
                    translateX: x
                    translateY: y
                }

            var txt : Label =
            Label
                {
                    layoutInfo: LayoutInfo
                    {
                        width: width
                    }
                    translateX: x - 10
                    translateY: y
                    text: {{numCivil[i]}}
                    textFill: Color.BLACK
                    font : Font
                    {
                        name: "Impact"
                        size: 18
                    }
                    hpos: HPos.CENTER
                }

                var bonus : Label =
                Label
                {
                    translateX: x-100
                    translateY: y
                    font: Font
                    {
                        name: "Impact"
                        size: 18
                    }
                    text: bind "Bonus : {{bonusPlaces[i]}}"
                    textFill: Color.BLUE
                }


            var civilSlot : Group =
            Group
            {
                onMouseEntered: function(e:MouseEvent) : Void
                {
                    if(isReduced == false)
                    {
                        rect.stroke = Color.RED;
                    }
                }
                onMouseExited: function(e:MouseEvent) : Void
                {
                    rect.stroke = Color.BLACK;
                }
                onMouseClicked: function(e:MouseEvent) : Void
                {
                    if(isReduced == false and isActive)
                    {
                        var selectedCiv = ControllerPrincipal.getINSTANCE().selectCivilization(i);
                        var numPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getNumCurrentPlayer();
                        var currentPlayer = ControllerPrincipal.getINSTANCE().getControllerGameBoard().getCurrentPlayer();

                        /* MAJ points, troupes, empire */
                        ViewPrincipal.getInstance().getGameScreen().slots[numPlayer].updateDisplay();
                        ViewPrincipal.getInstance().getGameScreen().slots[numPlayer].setCivilization(false, selectedCiv);
                        this.resetCivilization();
                        this.updateCivilization();
                        this.updateBonusPts();
                        ViewPrincipal.getInstance().getGameScreen().closeTurn();
                    }
                }
                
                content :
                [
                    rect, 
                    VBox
                    {
                        content : 
                        [
                            HBox
                            {
                                spacing: 10
                                content :
                                [
                                    txt, bonus
                                ]
                            }
                            HBox
                            {
                                spacing: 5
                                content :
                                [
                                    card1, card2
                                ]
                            }
                        ]
                    }
                ]
            }
            
            insert card1 into civilPlaces;
            insert card2 into civilPlaces;
            insert civilSlot into civilSlots;

            if(i == (numCivil.size()/2)-1)
            {
                x = 10;
                y = height + 60
            }
            else
            {
                x += width + 10
            }
        }
    }

//    public function loadCards() : Void
//    {
//        for(url in FileBrowser.listDirectory(Templates.CARD_PATH))
//        {
//            insert "file:///{{url}}" into cards;
//        }
//    }

    public function magnified(): Void
    {
        if(isReduced)
        {
            var gameScreen = ViewPrincipal.getInstance().getGameScreen();
            gameScreen.fadeMenu(gameScreen.openCivPicker, true, 0);
            translateUp.playFromStart();
            scaleUp.playFromStart();
            isReduced = false;
        }
    }

    public function miniaturized(): Void
    {
        if(isReduced == false)
        {
            var gameScreen = ViewPrincipal.getInstance().getGameScreen();
            gameScreen.fadeMenu(gameScreen.openCivPicker, false, 0.7);
            translateDown.playFromStart();
            scaleDown.playFromStart();
            isReduced = true;
        }
    }

    init
    {
        ini();
        translateX = x;
        translateY = y;
        content =
            [
                window,
                title,
                Group
                {
                    content :
                    [
                        reduceButton
                    ]
                }
                civilSlots
            ];
    }

    public function ini(): Void
    {
//        loadCards();
        translateDown.playFromStart();
        scaleDown.playFromStart();
        isReduced = true;
        buildPlaces();
    }

    public function close(): Void
    {
    }

}
